<?php
namespace app\common\model;

use think\Model;
use app\common\model\Channel as ChannelModel;

class Member extends Base
{
    protected $table = 'members';

    public function getCreateTimeLabelAttr($value, $data)
    {
        if (empty($data['create_time'])) {
            return '暂无';
        }

        return date('Y-m-d H:i:s', $data['create_time']);
    }

    public function getLastLoginTimeLabelAttr($value, $data)
    {
        if (empty($data['last_login_time'])) {
            return '暂未登录';
        }
        return date('Y-m-d H:i:s', $data['last_login_time']);
    }

    public function getLastLoginIpLabelAttr($value, $data)
    {
        if (empty($data['last_login_ip'])) {
            return '';
        }

        return long2ip(intval($data['last_login_ip']));
    }

    public function getIsRenzhengLabelAttr($value, $data)
    {
        $result = '';
        switch ($data['is_renzheng']) {
            case 0:
                $result = '未完成';
                break;
            case 1:
                $result = '已完成';
                break;
        }

        return $result;
    }

    public function getChannelNameAttr($value, $data)
    {
        $channel = ChannelModel::get(array('id' => $data['channel_id']));
        if (empty($channel)) {
            return '平台';
        }

        return $channel['name'];
    }

    public function getMemberProductTotalAttr($value, $data)
    {
        $count = MemberProduct::where('member_id', $data['id'])->count();

        return $count;
    }
    
    public function getPhoneLabelAttr($value, $data)
    {
        if (!isset($data['phone']) || empty($data['phone'])) {
            return '';
        }
        
        $phone = substr($data['phone'], 0, 3) . str_repeat('*', strlen($data['phone']) - 7) . substr($data['phone'], -4);
        
        return $phone;
    }

}